const query = require("../db/mysql")
const { error, success } = require("../module/module")



class Team {
    teamDate = async (ctx) => {
        let sql = `select * from teamBuy where 1=1 `
        let data = await query(sql)
        ctx.body = data
    }
    //添加团购
    addTeamBuy = async (ctx) => {
        let { name, title, time, address, img, peopleNum, state, teamcontext, userImg, phone } = ctx.request.body
        let findHave = `select * from teamBuy where 1=1 and title like "%${title}%" and name like "%${name}%" `
        let isHave = await query(findHave)
        if (!isHave.length) {
            let slq = `insert into teamBuy (name, title, time, address, img, peopleNum, state, teamcontext, userImg, phone) values ("${name}", "${title}", "${time}", "${address}", "${img}", "${peopleNum}", "${state}", "${teamcontext}", "${userImg}", "${phone}" )`
            let data = await query(slq)
            if (data.affectedRows === 1) {
                ctx.body = success(data, '添加成功')
            } else {
                ctx.body = error('添加失败')
            }

        } else {
            ctx.body = error('已存在此数据')
        }
    }
    //团购删除
    deleteTeam = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `delete from teamBuy where id=${id} `
        let data = await query(sql)

        if (data.affectedRows === 1) {
            ctx.body = success(data, '删除成功')
        } else {
            ctx.body = error("没有此数据，删除失败！")
        }
    }
}

module.exports = new Team()